# 维修订单模型
from .base import BaseModel
from .database import db
from datetime import datetime

class RepairOrder(BaseModel):
    __tablename__ = 'repair_orders'

    id = db.Column(db.Integer, primary_key=True, autoincrement=True, comment='订单ID（自增主键）')
    user_id = db.Column(db.Integer, nullable=False, comment='关联用户ID')
    address_id = db.Column(db.Integer, nullable=True, comment='关联维修地址ID（来自user_address表）')
    tech_id = db.Column(db.Integer, nullable=True, comment='关联师傅ID（来自technicians表）')
    status = db.Column(db.SmallInteger, default=0, comment='订单状态（0-待支付，1-已支付，2-已派给师傅，3-师傅已接单，4-维修中，5-已完成）')
    security_code = db.Column(db.String(255), nullable=True, comment='防伪码（图片路径或标识）')
    asset_name = db.Column(db.String(100), nullable=False, comment='资产名称（如：美的冰箱BCD-123）')
    damage_desc = db.Column(db.Text, nullable=True, comment='损坏说明')
    fault_code = db.Column(db.String(50), nullable=True, comment='故障代码（如：E01）')
    fault_detail = db.Column(db.Text, nullable=True, comment='故障内容详情')
    expected_time = db.Column(db.DateTime, nullable=True, comment='期望上门时间')
    contact_phone = db.Column(db.String(20), nullable=False, comment='联系方式（手机号）')
    repair_address = db.Column(db.String(255), nullable=True, comment='维修地址（冗余存储，避免地址变更影响订单）')
    remark = db.Column(db.Text, nullable=True, comment='备注')
    price = db.Column(db.Numeric(10, 2), nullable=True, comment='维修价格（元）')
    order_id = db.Column(db.String(50), nullable=True, comment='订单号（唯一标识）')
    extra_fee_id = db.Column(db.Integer, nullable=True, comment='关联额外费用ID（可关联其他费用表）')
    created_at = db.Column(db.DateTime, default=datetime.utcnow, comment='创建时间')
    updated_at = db.Column(db.DateTime, default=datetime.utcnow, onupdate=datetime.utcnow, comment='更新时间')
    # 移除is_deleted字段，因为DDL中没有
    is_deleted = None

    # 重写to_dict方法，排除is_deleted字段
    def to_dict(self):
        result = super().to_dict()
        result.pop('is_deleted', None)
        return result

    def __repr__(self):
        return f'<RepairOrder {self.id}>'